<template>
  <Modal v-model:visible="showModal" title="公路-客运量" width="60vw">
    <div class="flex flex-col">
      <div class="flex flex-row items-center mb-1">
        <a-select
          v-model:value="searchParams.year"
          :options="utils.genSelectOptions('year')"
          @change="loadData"
        ></a-select>
      </div>
      <vxe-table
        :loading="true"
        v-bind="customTableStyle"
        :data="tableData"
        class="custom-vxe-table"
      >
        <vxe-column field="year" title="年份" width="80">
          <template #default="{ row }"> {{ row.year }}年 </template>
        </vxe-column>
        <vxe-column field="month" title="月份" width="80" :sortable="true">
          <template #default="{ row }"> {{ row.month }}月 </template>
        </vxe-column>

        <vxe-column
          field="turnoverVolume"
          title="旅客周转量(万人公里)"
          minWidth="100"
          :sortable="true"
        >
          <template #default="{ row }">
            {{ row.turnoverVolume || 0 }}
          </template>
        </vxe-column>
        <vxe-column field="passengerVolume" title="客运量(万人)" minWidth="100" :sortable="true">
          <template #default="{ row }">
            {{ row.passengerVolume || 0 }}
          </template>
        </vxe-column>
        <vxe-column
          field="passengerVolumeTb"
          title="客运量同比增幅"
          minWidth="100"
          :sortable="true"
        >
          <template #default="{ row }">
            {{ row.passengerVolumeTb ? row.passengerVolumeTb.toFixed(2) + '%' : '/' }}
          </template>
        </vxe-column>
        <vxe-column
          field="passengerVolumeHb"
          title="客运量环比增幅"
          minWidth="100"
          :sortable="true"
        >
          <template #default="{ row }">
            {{ row.passengerVolumeHb ? row.passengerVolumeHb.toFixed(2) + '%' : '/' }}
          </template>
        </vxe-column>
        <!--        <vxe-column field="passengerCountPercentage" title="客运量占比" minWidth="100" :sortable="true">
          <template #default="{ row }">
            {{ row.passengerCountPercentage }}%
          </template>
        </vxe-column>-->
      </vxe-table>
    </div>
  </Modal>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import Modal from '@/components/modal.vue'
import { Apis } from '@/api'
import { message } from 'ant-design-vue'
import * as utils from '../../utils'

const isLoading = ref(false)

const showModal = ref(false)

const searchParams = ref({
  year: '',
})
const tableData = ref<any[]>([])

// 统一管理表格样式
const customTableStyle = {
  ...utils.customTableStyle,
  footerMethod: ({ columns, data }) => {
    const footerData = [
      columns.map((column, columnIndex) => {
        if (columnIndex === 0) {
          return '合计'
        }
        if (['passengerVolume'].includes(column.field)) {
          return (utils.sumNum(data, column.field) || '') + '万人'
        }
        if (['getOffNumber', 'getTheNumber'].includes(column.field)) {
          return (utils.sumNum(data, column.field) || '') + '万人'
        }
        if (['turnoverVolume'].includes(column.field)) {
          return (utils.sumNum(data, column.field) || '') + '万人公里'
        }

        return ''
      }),
    ]
    return footerData
  },
}

function loadData() {
  isLoading.value = true
  Apis.szbt.land
    .get_getChartData({
      params: {
        year: searchParams.value.year,
        month: '12',
        flag: 1,
      },
    })
    .then((res) => {
      tableData.value = (res || [])
        .filter((item) => item.year == searchParams.value.year)
        .sort((a: any, b: any) => {
          // 先按年份倒序
          if (b.month !== a.month) {
            return a.month - b.month
          }
        })
        .map((item: any, index: number) => {
          item.index = index + 1
          return item
        })

      const totalPassengerCount = tableData.value.reduce(
        (sum, item) => sum + (item.passengerVolume || 0),
        0,
      )
      tableData.value = tableData.value.map((item) => {
        const passengerCount = item.passengerVolume || 0
        item.passengerCountPercentage = totalPassengerCount
          ? ((passengerCount / totalPassengerCount) * 100).toFixed(2)
          : 0
        return item
      })
    })
    .catch((err) => {
      message.error('获取数据失败')
    })
    .finally(() => {
      isLoading.value = false
    })
}

defineExpose({
  setData: (data: any) => {
    const { params } = data
    searchParams.value = params
    // tableData.value = data;
  },
  open: () => {
    showModal.value = true
    loadData()
  },
})
</script>

<style lang="scss" src="@/styles/custom-vxe-table.scss"></style>
